#!/usr/bin/env python
# -*- coding: GBK -*-
#SMTP_email.py
#auth@:xfk
#date@:2012-04-30
import smtplib, mimetypes
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import os

class smtpclient:
    port=25
    stmpAddr= None
    username=None
    password=None
    localMaill=None
    mailserver=None
    debugLevel=0
    def init_smtp(self):
        pass
    def relase_smtp(self):
        if self.mailserver != None :
            self.mailserver.close()
                            
    def set_debugLevel(self,_level):
        self.debugLevel = _level
    def open_smtp(self,_host,_port):
        self.host = _host
        self.port = _port
        try:
            self.mailserver=smtplib.SMTP(_host,_port)
            if self.debugLevel > 0:
                print("smtplib init Ok")
            return True
        except:
            self.mailserver=None
            if self.debugLevel > 0:
                print("smtplib init error")
            return False
    def login_mail(self,_user,_password):
        if self.mailserver != None:
            self.username = _user
            self.password = _password
            try:
                self.mailserver.login(_user, _password)
                if self.debugLevel > 0:
                    print("smtplib login Ok")
                return True
            except:
                if self.debugLevel > 0:
                    print("smtplib login error")
                return False
        else:
            return False
    def close_mail(self):
        if self.mailserver != None:
            self.mailserver.close()
            if self.debugLevel > 0:
                print("close mail Ok \n")
            return True
        else:
            return False
    def send_mail(self,_from,_to,_title,_msg):
        if self.mailserver != None:
            try:
                #mail head
                msg = MIMEMultipart()
                msg['From'] = _from
                msg['To'] = _to
                msg['Subject'] = _title
                #mail msg
                txt = MIMEText(_msg)
                msg.attach(txt)
                filename=r'E:\\Temp\\2.jpg'
                ctype,encoding = mimetypes.guess_type(filename)
                if ctype is None or encoding is not None:
                    ctype='application/octet-stream'
                maintype,subtype = ctype.split('/',1)
                att=MIMEImage(open(filename, 'rb').read(),subtype)
                att.add_header('Content-ID', '<2.jpg>')
                print ctype,encoding
                att["Content-Disposition"] = 'inline;filename=2.jpg'
                msg.attach(att)
                self.mailserver.sendmail(_from, _to, msg.as_string())
                if self.debugLevel > 0:
                    print("smtplib send Ok")
                return True
            except:
                if self.debugLevel > 0:
                    print("smtplib send error")
                return False
        else:
            return False
                    
mail =smtpclient()
mail.set_debugLevel(1)
mail.open_smtp("smtp.189.cn", 25)
mail.login_mail("18959130026@189.cn", "523426")
mail.send_mail("18959130026@189.cn", "18959130026@189.cn", "Demo for python1", "hello wolrd\n")
#mail.send_mail("18959130026@189.cn", "linh@ecallcen.com", "Demo for python", "hello wolrd\n")
mail.close_mail()



